package Dragons;

import java.util.ArrayList;

import jgame.JGPoint;
import jgame.JGTimer;
import Main.Game;
import Other.Field;
import Units.Dragon;
import Units.UnitName;

public class Dragon3 extends Dragon {
	public static int imageHeight = 86, imageWidth = 74;
	
	private final static String stringStandingDown = "dragon3",
								stringGoDown	   = "robot1animgo_down",
								stringStopDown	   = "robot1animstop_down",
								stringMoveDown	   = "robot1animmove_down";
	
	public Dragon3(String name, boolean unique, double posX, double posY, int cid){
		super(name, unique, posX, posY, cid, stringStandingDown);
	}

	/*@Override
	public boolean canMove(Field field) {
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( Game.board.getUnitName(field.i, field.j) != UnitName.EMPTY ) return false;
		
		boolean can = false;
		for (int dir=0; dir<Field.adjacentFields.length; dir++) {
			if (field.i == this.getField().i + Field.adjacentFields[dir][0] && field.j == this.getField().j + Field.adjacentFields[dir][1]) can = true;
		}
		return can;
	}*/
	@Override
	public boolean canMove(Field field) {
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( UnitName.isDragon(Game.board.getUnitName(field.i, field.j)) ) return false;
		
		boolean can = false;
		for (int i=0; i<Field.adjacentFields.length; i++) {
			Field firstMove = new Field(this.getField().i + Field.adjacentFields[i][0], 
										this.getField().j + Field.adjacentFields[i][1]);
			
			if (field.i==firstMove.i && field.j==firstMove.j) can = true; 
			
			for (int j=-1; j<2; j++){
				int nextDirection = (i+j+Field.adjacentFields.length)%Field.adjacentFields.length;
				Field secondMove = new Field(firstMove.i + Field.adjacentFields[nextDirection][0], 
											 firstMove.j + Field.adjacentFields[nextDirection][1]);
				if (field.i == secondMove.i && field.j == secondMove.j) can = true; 
			}
		}
		return can;
	}
	
	@Override
	public ArrayList<Field> whereAttack(Field field) {
		
		ArrayList<Field> ret = new ArrayList<Field>();
		
		ret.clear();
		
		if (!Game.board.inBoard(field.i, field.j)) return ret;
		if ( !UnitName.isRobot(Game.board.getUnitName(field.i, field.j)) ) return ret;
		
		for (int dir=0; dir<Field.adjacentFields.length; dir++) {			
			int di = Field.adjacentFields[dir][0];
			int dj = Field.adjacentFields[dir][1];
			
			Field p1 = new Field(this.getField().i + di, this.getField().j + dj);
			Field p2 = new Field(this.getField().i + 2*di, this.getField().j + 2*dj);
			Field p3 = new Field(this.getField().i + 3*di, this.getField().j + 3*dj);
			
			if (field.compare(p1) || field.compare(p2) || field.compare(p3)) {
				if (UnitName.isRobot(Game.board.getUnitName(p1.i, p1.j))) ret.add(p1);
				if (UnitName.isRobot(Game.board.getUnitName(p2.i, p2.j))) ret.add(p2);
				if (UnitName.isRobot(Game.board.getUnitName(p3.i, p3.j))) ret.add(p3);
			}
					
		}
		
		return ret;
	}
	
	@Override
	public boolean canAttack(Field field) {
		// TODO Auto-generated method stub
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( !UnitName.isRobot(Game.board.getUnitName(field.i, field.j)) ) return false;
		
		boolean ret = false;
		
		for (int dir=0; dir<Field.adjacentFields.length; dir++) {			
			int di = Field.adjacentFields[dir][0];
			int dj = Field.adjacentFields[dir][1];
			
			Field p1 = new Field(this.getField().i + di, this.getField().j + dj);
			Field p2 = new Field(this.getField().i + 2*di, this.getField().j + 2*dj);
			Field p3 = new Field(this.getField().i + 3*di, this.getField().j + 3*dj);
			
			if (field.compare(p1) || field.compare(p2) || field.compare(p3)) {
				ret = true;
			}
					
		}
		
		return ret;
	}

	public void move() {
		switch (getState()){
			case 0:
				setGraphic(stringStandingDown);
				break;
			case 1:
				setGraphic(stringGoDown);
				break;
			case 2:
				setGraphic(stringMoveDown);
				JGPoint p = Game.board.getRealCoordinates(getTargetField());
				
				if (Game.board.distance(p, new JGPoint((int)getLastX() + this.imageWidth/2, (int)getLastY() + this.imageHeight/2)) < 5){
					xspeed = yspeed = 0;
					setState(3);
					new JGTimer(5, true){

						@Override
						public void alarm() {
							setState(0);
							Game.board.setAnimation(false);
							Field f = Dragon3.this.getField();
							Field t = Dragon3.this.getTargetField();
							
							Game.board.moveFromTo(f, t);
							Dragon3.this.setField(Dragon3.this.getTargetField());
						}						
					};
				}
				break;
			case 3:
				setGraphic(stringStopDown);
				break;
			default: setGraphic(stringStandingDown);
		}
	}
}
